home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / src / pixmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-21  |  6.8 KB  |  272 lines

  1. /*
  2.  *  Window Maker window manager
  3.  * 
  4.  *  Copyright (c) 1997, 1998 Alfredo K. Kojima
  5.  * 
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
  19.  *  USA.
  20.  */
  21.  
  22. #include "wconfig.h"
  23.  
  24. #include <X11/Xlib.h>
  25. #include <X11/Xutil.h>
  26.  
  27. #include <wraster.h>
  28.  
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "WindowMaker.h"
  32. #include "wcore.h"
  33.  
  34.  
  35.  
  36. /*
  37.  *---------------------------------------------------------------------- 
  38.  * wPixmapCreateFromXPMData--
  39.  *     Creates a WPixmap structure and initializes it with the supplied
  40.  * XPM structure data.
  41.  * 
  42.  * Returns:
  43.  *     A WPixmap structure or NULL on failure.
  44.  * 
  45.  * Notes:
  46.  *     DEF_XPM_CLOSENESS specifies the XpmCloseness
  47.  *---------------------------------------------------------------------- 
  48.  */
  49. WPixmap *
  50. wPixmapCreateFromXPMData(WScreen *scr, char **data)
  51. {
  52.     RImage *image;
  53.     WPixmap *pix;
  54.  
  55.     image = RGetImageFromXPMData(scr->rcontext, data);
  56.     if (!image)
  57.     return NULL;
  58.  
  59.     pix = wmalloc(sizeof(WPixmap));
  60.     memset(pix, 0, sizeof(WPixmap));
  61.  
  62.     RConvertImageMask(scr->rcontext, image, &pix->image, &pix->mask, 128);
  63.     
  64.     pix->width = image->width;
  65.     pix->height = image->height;
  66.     pix->depth = scr->w_depth;
  67.     
  68.     RDestroyImage(image);
  69.     
  70.     return pix;
  71. }
  72.  
  73.  
  74. /*
  75.  *---------------------------------------------------------------------- 
  76.  * wPixmapCreateFromXBMData--
  77.  *     Creates a WPixmap structure and initializes it with the supplied
  78.  * XBM structure data, size and mask.
  79.  * 
  80.  * Returns:
  81.  *     A WPixmap structure or NULL on failure.
  82.  * 
  83.  *---------------------------------------------------------------------- 
  84.  */
  85. WPixmap *
  86. wPixmapCreateFromXBMData(WScreen *scr, char *data, char *mask, 
  87.              int width, int height, unsigned long fg,
  88.              unsigned long bg)
  89. {
  90.     WPixmap *pix;
  91.     
  92.     pix = wmalloc(sizeof(WPixmap));
  93.     memset(pix, 0, sizeof(WPixmap));
  94.     pix->image = XCreatePixmapFromBitmapData(dpy, scr->w_win, data, width, 
  95.                          height, fg, bg, scr->w_depth);
  96.     if (pix->image==None) {
  97.     free(pix);
  98.     return NULL;
  99.     }
  100.     if (mask) {
  101.     pix->mask = XCreateBitmapFromData(dpy, scr->w_win, mask, width, 
  102.                       height);
  103.     } else {
  104.     pix->mask = None;
  105.     }
  106.     pix->width = width;
  107.     pix->height = height;
  108.     pix->depth = scr->w_depth;
  109.     return pix;
  110. }
  111.  
  112.  
  113. #ifdef unused
  114. WPixmap*
  115. wPixmapCreateFromBitmap(WScreen *scr, Pixmap bitmap, Pixmap mask,
  116.             unsigned long fg, unsigned long bg)
  117. {
  118.     WPixmap *pix;
  119.     XImage *img, *img2;
  120.     Window foo;
  121.     int bar, x, y;
  122.     Pixmap pixmap;
  123.     unsigned int width, height, baz, d;
  124.  
  125.     if (!XGetGeometry(dpy, bitmap, &foo, &bar, &bar, &width, &height, &baz, 
  126.               &d) || d!=1) {
  127.     return NULL;
  128.     }
  129.     img = XGetImage(dpy, bitmap, 0, 0, width, height, AllPlanes, XYPixmap);
  130.     if (!img)
  131.       return NULL;
  132.     
  133.     img2=XCreateImage(dpy, scr->w_visual, scr->w_depth, ZPixmap,
  134.               0, NULL, width, height, 8, 0);
  135.     if (!img2) {
  136.     XDestroyImage(img);
  137.     return NULL;
  138.     }
  139.     
  140.     pixmap = XCreatePixmap(dpy, scr->w_win, width, height, scr->w_depth);
  141.     if (pixmap==None) {
  142.     XDestroyImage(img);
  143.     XDestroyImage(img2);
  144.     return NULL;
  145.     }
  146.  
  147.     img2->data = wmalloc(height * img2->bytes_per_line);
  148.     
  149.     for (y=0; y<height; y++) {
  150.     for (x=0; x<width; x++) {
  151.         if (XGetPixel(img, x, y)==0) {
  152.         XPutPixel(img2, x, y, bg);
  153.         } else {
  154.         XPutPixel(img2, x, y, fg);
  155.         }
  156.     }
  157.     }
  158.     XSetClipMask(dpy, scr->copy_gc, None);
  159.     XPutImage(dpy, pixmap, scr->copy_gc, img2, 0, 0, 0, 0, width, height);
  160.     XDestroyImage(img);
  161.     XDestroyImage(img2);
  162.  
  163.     pix = wmalloc(sizeof(WPixmap));
  164.     memset(pix, 0, sizeof(WPixmap));
  165.     pix->image = pixmap;
  166.     pix->mask = mask;
  167.     pix->width = width;
  168.     pix->height = height;
  169.     pix->depth = scr->w_depth;
  170.     return pix;
  171. }
  172. #endif /* unused */
  173.  
  174. WPixmap*
  175. wPixmapCreate(WScreen *scr, Pixmap image, Pixmap mask)
  176. {
  177.     WPixmap *pix;
  178.     Window foo;
  179.     int bar;
  180.     unsigned int width, height, depth, baz;
  181.     
  182.     pix = wmalloc(sizeof(WPixmap));
  183.     memset(pix, 0, sizeof(WPixmap));
  184.     pix->image = image;
  185.     pix->mask = mask;
  186.     if (!XGetGeometry(dpy, image, &foo, &bar, &bar, &width, &height, &baz,
  187.      &depth)) {
  188.     wwarning("XGetGeometry() failed during wPixmapCreate()");
  189.     free(pix);
  190.     return NULL;
  191.     }
  192.     pix->width = width;
  193.     pix->height = height;
  194.     pix->depth = depth;
  195.     return pix;
  196. }
  197.  
  198. #if 0
  199. /*
  200.  *---------------------------------------------------------------------- 
  201.  * wPixmapLoadXBMFile--
  202.  *     Creates a WPixmap structure and loads a XBM file into it with
  203.  * an optional mask file. If a mask is not wanted, mask_path should be
  204.  * NULL.
  205.  * 
  206.  * Returns:
  207.  *     A WPixmap structure or NULL on failure.
  208.  * 
  209.  * Notes:
  210.  *     If the mask bitmap is not successfully loaded the operation
  211.  * continues as no mask was supplied.
  212.  *---------------------------------------------------------------------- 
  213.  */
  214. WPixmap *
  215. wPixmapLoadXBMFile(WScreen *scr, char *path, char *mask_path)
  216. {
  217.     WPixmap *pix;
  218.     int junk;
  219.  
  220.     if (!path) return NULL;
  221.     
  222.     pix = wmalloc(sizeof(WPixmap));
  223.     memset(pix, 0, sizeof(WPixmap));
  224.     
  225.     if (XReadBitmapFile(dpy, scr->w_win, path, (unsigned *)&(pix->width), 
  226.             (unsigned *)&(pix->height),
  227.             &(pix->image), &junk, &junk)!=BitmapSuccess) {
  228.     free(pix);
  229.     return NULL;
  230.     }
  231.     if (mask_path!=NULL) {
  232.     if (XReadBitmapFile(dpy, scr->w_win, path, (unsigned *)&junk, 
  233.                 (unsigned *)&junk, &(pix->mask), 
  234.                 &junk, &junk) !=BitmapSuccess) {
  235.         wwarning(_("could not load mask bitmap file \"%s\". Won't use mask"),
  236.              mask_path);
  237.         pix->mask = None;
  238.     }
  239.     } else {
  240.     pix->mask = None;
  241.     }
  242.     pix->depth = 1;
  243.     return pix;
  244. }
  245.  
  246. #endif 
  247.  
  248. /*
  249.  *---------------------------------------------------------------------- 
  250.  * wPixmapDestroy--
  251.  *     Destroys a WPixmap structure and the pixmap/mask it holds.
  252.  * 
  253.  * Returns:
  254.  *     None
  255.  *----------------------------------------------------------------------
  256.  */
  257. void 
  258. wPixmapDestroy(WPixmap *pix)
  259. {
  260.     if (!pix->shared) {
  261.     if (pix->mask && !pix->client_owned_mask) {
  262.         XFreePixmap(dpy, pix->mask);
  263.     }
  264.  
  265.     if (pix->image && !pix->client_owned) {
  266.         XFreePixmap(dpy, pix->image);
  267.     }
  268.     }
  269.     free(pix);
  270. }
  271.  
  272.